package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.client.ManagementClient;
import com.woniu.client.ProjectClient;
import com.woniu.command.Command;
import com.woniu.command.orderCommand.*;
import com.woniu.inlet.group.AddGroup;
import com.woniu.inlet.vo.CustomerVo;
import com.woniu.inlet.vo.OrderVo;
import com.woniu.outlet.po.OrderDetails;
import com.woniu.outlet.po.OrderPo;
import com.woniu.service.OrderService;
import com.woniu.util.ResponseResult;
import com.woniu.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "订单管理")
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    @Qualifier("orderPtoject")
    private ProjectClient projectClient;

    @Autowired
    @Qualifier("orderManagement")
    private ManagementClient managementClient;

    @GetMapping("getCustomerById")
    @ApiOperation("根据id查询客户")
    public ResponseResult<CustomerVo> getCustomerById(Long id){
        CustomerVo customerVo = managementClient.getCustomerById(id).getData();
        return new ResponseResult<>(customerVo,"查询客户成功", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("findIdByOrderNo")
    @ApiOperation("查询订单id(根据订单编号)")
    public ResponseResult<Long> findIdByOrderNo(String orderNo)throws Exception{
        Long id = orderService.findIdByOrderNo(orderNo);
        return new ResponseResult<>(id, "查询成功", ResultCode.SUCCESS.getCode());
    }

        //查询所有订单
    @GetMapping("findOrders")
    @ApiOperation("查询全部订单")
    public ResponseResult<List<OrderVo>> findOrders() throws Exception{
       List<OrderPo> orderPos = orderService.findOrders();
       List<OrderVo> orderVos = new ArrayList<>();
       //对象的深拷贝
        for (OrderPo item:orderPos){
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(item,orderVo);
            orderVos.add(orderVo);
        }
        return new ResponseResult<>(orderVos, "OK", ResultCode.SUCCESS.getCode());
    }

    //根据订单编号查询订单商品明细
    @GetMapping("findOrderByOrderNo")
    @ApiOperation("查询订单及明细(根据订单编号)")
    public ResponseResult<List<OrderPo>> findOrderByOrderNo(String orderNo) throws Exception{
        List<OrderPo> orderPos = orderService.findOrderByOrderNo(orderNo);
        return new ResponseResult<>(orderPos, "OK", ResultCode.SUCCESS.getCode());
    }

    //根据订单编号查询订单商品明细
//    @GetMapping("findOrderById")
//    @ApiOperation("查询订单及明细(根据订单id)")
    public ResponseResult<List<OrderPo>> findOrderById(Long id) throws Exception{
        List<OrderPo> orderPos = orderService.findOrderById(id);
        return new ResponseResult<>(orderPos, "OK", ResultCode.SUCCESS.getCode());
    }

    //添加订单
    @PostMapping("addOrder")
    @ApiOperation("添加订单")
    @ApiOperationSupport(ignoreParameters = {"id","orderNo","createDate","modifyDate","endDate","expiryDate",
            "spare1","spare2","spare3"})
    public ResponseResult<String> addOrder(@Validated({AddGroup.class}) OrderPo orderPo,BindingResult result){
        if(result.hasErrors()){
            return new ResponseResult<>(ResultCode.PARAM_TYPE_ERROR.getCode(),ResultCode.PARAM_TYPE_ERROR.getMessage(),"添加失败");
        }
        Command command = new AddOrderCommand();

        boolean flag = (boolean)command.excute(orderPo);
        if(flag){
            return new ResponseResult<>( ResultCode.SUCCESS.getCode(),"添加订单成功");
        }
        return new ResponseResult<>( ResultCode.CUSTOMER_ID_IS_NOT_EXIST.getCode(),ResultCode.CUSTOMER_ID_IS_NOT_EXIST.getMessage(),"添加订单失败");
    }


    //添加订单明细
    @PostMapping("addOrderDetails")
    @ApiOperation("添加订单明细")
    @ApiOperationSupport(ignoreParameters = {"id","spare1","spare2","spare3"})
    public ResponseResult<String> addOrderDetails(@Valid OrderDetails orderDetails,BindingResult result){
        if(result.hasErrors()){
            return new ResponseResult<>(ResultCode.PARAM_TYPE_ERROR.getCode(),ResultCode.PARAM_TYPE_ERROR.getMessage(),"添加失败");
        }
        Command command = new AddOrderDetailsCommand();
        boolean flag = (boolean) command.excute(orderDetails);
        if(flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"添加订单明细成功");
        }
        return new ResponseResult<>(ResultCode.PROJECT_ID_IS_NOT_EXIST.getCode(),ResultCode.PROJECT_ID_IS_NOT_EXIST.getMessage(),"添加订单明细失败");
    }

    //修改订单信息
    @PostMapping("updateOrder")
    @ApiOperation("修改订单")
    @ApiOperationSupport(ignoreParameters = {"createDate","orderNo","spare1","spare2","spare3"})
    public ResponseResult<String> updateOrder(@Valid OrderPo OrderPo){
        Command command = new UpdateOrderCommand();
        boolean flag = (boolean)command.excute(OrderPo);
        if(flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"修改订单成功");
        }
        return new ResponseResult<>(ResultCode.ID_IS_NOT_EXIST.getCode(),ResultCode.ID_IS_NOT_EXIST.getMessage(),"修改订单失败");
    }

    //删除订单信息
    @DeleteMapping("deleteOrderByOrderNo")
    @ApiOperation("删除订单(根据订单编号)")
    public ResponseResult<String> deleteOrder(String orderNo){
        Command command = new DeleteOrderCommand();
        boolean flag = (boolean)command.excute(orderNo);
        if (flag){
            return new ResponseResult<>(ResultCode.SUCCESS.getCode(),"删除订单成功");
        }
        return new ResponseResult<>(ResultCode.ORDERNO_IS_NOT_EXIST.getCode(),ResultCode.ORDERNO_IS_NOT_EXIST.getMessage(),"删除订单失败");
    }


}
